package com.tecsoluction.transportecerto;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class LocalOverlay extends Overlay {
	private GeoPoint geoPoint;
	private Paint paint = new Paint();
	private int imagem;
	
	
	public LocalOverlay (GeoPoint geoPoint, int imagem)
	{
		this.geoPoint=geoPoint;
		this.imagem=imagem;
	}

	public LocalOverlay(GeoPoint geoPoint2) 
	{
		this.geoPoint=geoPoint2;
	}
	public void draw(Canvas canvas,MapView mapview,boolean boleano)
	{
	
		super.draw(canvas, mapview, boleano);
		
		if(geoPoint!= null)
		{
			
			//convertendo as coordenas para pixels
			Point ponto= mapview.getProjection().toPixels(geoPoint, null);
			//convertendo a imagem para bitmap
			Bitmap bitmap = BitmapFactory.decodeResource(mapview.getResources(),this.imagem);
			//classe rectf calcula o tamanho e altura da imagem
			RectF rectf = new RectF(ponto.x,ponto.y,ponto.x+bitmap.getWidth(),ponto.y+bitmap.getHeight());
			
		//desenhando o formato do ddesenho
			canvas.drawBitmap(bitmap, null, rectf, paint);
		} 
	}
	
	public boolean onTap(GeoPoint geopoint,MapView mapview)
	{
		Point ponto=mapview.getProjection().toPixels(this.geoPoint, null);
		RectF rectf = new RectF(ponto.x-15,ponto.y-15,ponto.x+15,ponto.y+15);
		Point novoPonto = mapview.getProjection().toPixels(geopoint, null);
		
		boolean localClick =rectf.contains(novoPonto.x,novoPonto.y);
		if(localClick == true)
		{
			Toast.makeText(mapview.getContext(),"Local Clicado :" + geopoint , Toast.LENGTH_SHORT).show();
			return true;
		}
	
	
			return super .onTap(geoPoint, mapview);
		
	}
	
	public void setGeopoint(GeoPoint geopoint)
			
			{
			this.geoPoint=geopoint;
			}
}
